package org.pjlib.common;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;

import org.pjlib.R;

/**
 * Created by ws on 2019/04/04.
 */
public class BViewHolder extends RecyclerView.ViewHolder {
    public BViewHolder(@NonNull View itemView) {
        super(itemView);
    }

   
    public <T extends View> T findViewById(int id) {
        if(itemView==null){
            return null;
        }
        return itemView.findViewById(id);
    }

   
    public BViewHolder tag(int id, Object object) {
        View view=findViewById(id);
        if(view!=null){
            view.setTag(object);
        }
        return this;
    }

   
    public BViewHolder text(int id, @StringRes int res) {
        TextView tv= (TextView) findViewById(id);
        if(tv!=null){
            tv.setText(res);
        }
        return this;
    }

   
    public BViewHolder text(int id, CharSequence charSequence) {
        TextView tv=  findViewById(id);
        if(tv!=null){
            tv.setText(charSequence);
        }
        return this;
    }


   
    public BViewHolder textColor(int id, int color) {
        TextView tv= (TextView) findViewById(id);
        if(tv!=null){
            tv.setTextColor(color);
        }
        return this;
    }

   
    public BViewHolder textSize(int id, int sp) {
        TextView tv= (TextView) findViewById(id);
        if(tv!=null){
            tv.setTextSize(sp);
        }
        return this;
    }

   
    public BViewHolder alpha(int id, float alpha) {
        View view=findViewById(id);
        if(view!=null){
            view.setAlpha(alpha);
        }
        return this;
    }

   
    public BViewHolder image(int id, int res) {
        ImageView iv= (ImageView) findViewById(id);
        if(iv!=null){
            iv.setImageResource(res);
        }
        return this;
    }

   
    public BViewHolder image(int id, Drawable drawable) {
        ImageView iv= (ImageView) findViewById(id);
        if(iv!=null){
            iv.setImageDrawable(drawable);
        }
        return this;
    }
    public BViewHolder image(int id, String url) {
        ImageView iv= findViewById(id);
        if(iv!=null ){
            if(url==null){
                url="";
            }
            ImgUtils.Glide(iv,url);
        }
        return this;
    }
    public BViewHolder avatar(int id, String url){
        ImageView iv= findViewById(id);
        if(iv!=null ){
            if(url==null){
                url="";
            }
            ImgUtils.Glide(iv,url,ImgUtils.getOption(R.drawable.ic_avatar_set,0));
        }
        return this;
    }
    public BViewHolder image(int id, Object url) {
        ImageView iv=  findViewById(id);
        if(iv!=null && !ObjectUtils.isEmpty(url)){
            ImgUtils.Glide(iv,url);
        }
        return this;
    }
    public BViewHolder background(int id, @DrawableRes int res) {
        View view=findViewById(id);
        if(view!=null){
            view.setBackgroundResource(res);
        }
        return this;
    }

   
    public BViewHolder background(int id, Drawable drawable) {
        View view=findViewById(id);
        if(view!=null){
            view.setBackground(drawable);
        }
        return this;
    }

   
    public BViewHolder visible(int id) {
        View view=findViewById(id);
        if(view!=null){
            view.setVisibility(View.VISIBLE);
        }
        return this;
    }

   
    public BViewHolder invisible(int id) {
        View view=findViewById(id);
        if(view!=null){
            view.setVisibility(View.INVISIBLE);
        }
        return this;
    }

   
    public BViewHolder gone(int id) {
        View view=findViewById(id);
        if(view!=null){
            view.setVisibility(View.GONE);
        }
        return this;
    }

   
    public BViewHolder visibility(int id, int visibility) {
        View view=findViewById(id);
        if(view!=null){
            view.setVisibility(visibility);
        }
        return this;
    }

   
    public BViewHolder clicked(int id, View.OnClickListener listener) {
        View view=findViewById(id);
        if(view!=null){
            view.setOnClickListener(listener);
        }
        return this;
    }

   
    public BViewHolder longClicked(int id, View.OnLongClickListener listener) {
        View view=findViewById(id);
        if(view!=null){
            view.setOnLongClickListener(listener);
        }
        return this;
    }

   
    public BViewHolder enable(int id, boolean enable) {
        View view=findViewById(id);
        if(view!=null){
            view.setEnabled(enable);
        }
        return this;
    }

   
    public BViewHolder enable(int id) {
        enable(id,true);
        return this;
    }

   
    public BViewHolder disable(int id) {
        enable(id,false);
        return this;
    }

   
    public BViewHolder checked(int id, boolean checked) {
        View view=findViewById(id);
        if(view!=null){
            view.setClickable(checked);
        }
        return this;
    }

   
    public BViewHolder selected(int id, boolean selected) {
        View view=findViewById(id);
        if(view!=null){
            view.setSelected(selected);
        }
        return this;
    }

   
    public BViewHolder pressed(int id, boolean pressed) {
        View view=findViewById(id);
        if(view!=null){
            view.setPressed(pressed);
        }
        return this;
    }
    public BViewHolder addView(int id, View... views) {
        if( findViewById(id) instanceof ViewGroup){
            ViewGroup view= (ViewGroup) findViewById(id);
            if(view!=null){
                if(views.length>0){
                    for (View v:views
                    ) {
                        view.addView(v);
                    }
                }
            }
        }
        return this;

    }

   
    public BViewHolder addView(int id, View view, ViewGroup.LayoutParams params) {
        return this;
    }

   
    public BViewHolder removeAllViews(int id) {
        if( findViewById(id) instanceof ViewGroup){
            ViewGroup view= (ViewGroup) findViewById(id);
            if(view!=null){
                view.removeAllViews();
            }
        }
        return this;
    }

   
    public BViewHolder removeView(int id, View v) {
        if( findViewById(id) instanceof ViewGroup){
            ViewGroup view= (ViewGroup) findViewById(id);
            if(view!=null){
                view.removeView(v);
            }
        }
        return this;
    }

   
}
